﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tools = Microsoft.Office.Tools.Word;
using Microsoft.Office.Interop.Word;
using System.Threading;
using TestPlanWordWriter.Common.Word;

namespace TestPlanWordWriter.UI
{
    public partial class SettingsForm : Form
    {
        public SettingsForm()
        {
            InitializeComponent();
        }

        private void SettingsForm_Load(object sender, EventArgs e)
        {
            PopulateCombos();
            PopulateLiterals();
            PopulateTestSuiteStyles();
            PositionCombo(cboTestPlan, Settings.TestPlanStyleName);
            PositionCombo(cboTestPlanDescription, Settings.TestPlanDescriptionStyleName);
            PositionCombo(cboTestCaseStyle, Settings.TestCaseStyleName);
            PositionCombo(cboTable, Settings.TestStepsTableStyleName);
            PositionCombo(cboTestCaseDescription, Settings.TestCaseDescriptionStyleName);
        }

        private void PopulateLiterals()
        {
            txtStepNumberLiteral.Text = Settings.TableLabel_Step;
            txtActionLiteral.Text = Settings.TableLabel_Action;
            txtExpectedResultLiteral.Text = Settings.TableLabel_ExpectedResult;
        }

        public List<string> TableStyles { get; set; }
        public List<string> ParagraphStyles { get; set; }

        private DataGridViewComboBoxColumn CreateComboBoxColumn(string header)
        {
            DataGridViewComboBoxColumn column =
                new DataGridViewComboBoxColumn();
            {
                column.HeaderText = header;
                column.DropDownWidth = 160;
                column.Width = 200;
                column.MaxDropDownItems = 3;
                column.FlatStyle = FlatStyle.Flat;
            }
            return column;
        }

        private void PopulateTestSuiteStyles()
        {
            DataGridViewComboBoxColumn titleColumn = CreateComboBoxColumn("Title Style");
            titleColumn.DataPropertyName = "TestSuiteStyleName";
            DataGridViewComboBoxColumn descriptionColumn = CreateComboBoxColumn("Description Style");
            descriptionColumn.DataPropertyName = "TestSuiteDescription";
            grdTestSuiteStyles.Columns.Add(titleColumn);
            grdTestSuiteStyles.Columns.Add(descriptionColumn);

            Microsoft.Office.Interop.Word.Document currentDocument = Globals.ThisAddIn.Application.ActiveDocument;
            foreach (Style documentStyle in currentDocument.Styles)
            {
                if (documentStyle.Type == WdStyleType.wdStyleTypeParagraph)
                {
                    titleColumn.Items.Add(documentStyle.NameLocal);
                    descriptionColumn.Items.Add(documentStyle.NameLocal);
                }
            }

            foreach (TestSuiteSettings suiteSettings in Settings.TestSuiteLevelsSettings)
            {
                grdTestSuiteStyles.Rows.Add(suiteSettings.TestSuiteStyleName, suiteSettings.TestSuiteDescription);
            }
        }

        private void PopulateCombos()
        {
            Microsoft.Office.Interop.Word.Document currentDocument = Globals.ThisAddIn.Application.ActiveDocument;
            foreach (Style documentStyle in currentDocument.Styles)
            {
                if (documentStyle.Type == WdStyleType.wdStyleTypeParagraph)
                {
                    cboTestPlan.Items.Add(documentStyle.NameLocal);
                    cboTestPlanDescription.Items.Add(documentStyle.NameLocal);
                    cboTestCaseStyle.Items.Add(documentStyle.NameLocal);
                    cboTestCaseDescription.Items.Add(documentStyle.NameLocal);
                }
                if (documentStyle.Type == WdStyleType.wdStyleTypeTable)
                {
                    cboTable.Items.Add(documentStyle.NameLocal);
                }
            }
        }

        private void PositionCombo(ComboBox comboToCheck, string settingValue)
        {
            for (int itemIndex = 0; itemIndex < comboToCheck.Items.Count; itemIndex++)
            {
                string itemString = (string)comboToCheck.Items[itemIndex];

                if (itemString.Equals(settingValue, StringComparison.InvariantCultureIgnoreCase))
                {
                    comboToCheck.SelectedIndex = itemIndex;
                    break;
                }
            }
        }

        public TestPlanDocWriterSettings Settings { get; set; }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Settings.TestPlanStyleName = cboTestPlan.Text;
            Settings.TestPlanDescriptionStyleName = cboTestPlanDescription.Text;

            Settings.TestStepsTableStyleName = cboTable.Text;
            
            Settings.TestCaseStyleName = cboTestCaseStyle.Text;
            Settings.TestCaseDescriptionStyleName = cboTestCaseDescription.Text;

            Settings.TableLabel_Step = txtStepNumberLiteral.Text;
            Settings.TableLabel_Action = txtActionLiteral.Text;
            Settings.TableLabel_ExpectedResult = txtExpectedResultLiteral.Text;

            List<TestSuiteSettings> suiteLevelsSettings = new List<TestSuiteSettings>();
            foreach (DataGridViewRow row in grdTestSuiteStyles.Rows)
            {
                if (!row.IsNewRow)
                {
                    TestSuiteSettings suiteSettings = new TestSuiteSettings();
                    suiteSettings.TestSuiteStyleName = (string)row.Cells[0].Value;
                    suiteSettings.TestSuiteDescription = (string)row.Cells[1].Value;
                    suiteLevelsSettings.Add(suiteSettings);
                }
            }
            Settings.TestSuiteLevelsSettings = suiteLevelsSettings;
        }

        private void tabTestSuites_Click(object sender, EventArgs e)
        {

        }
    }
}
